package com.shimao.celnet.payment.quartz;

import com.shimao.celnet.payment.config.ConstantConfig;
import com.shimao.celnet.payment.model.request.CallOut_OutInvoiceSumRequest;
import com.shimao.celnet.payment.model.request.CallOut_OutPaySumRequest;
import com.shimao.celnet.payment.service.PInvoiceDaySumService;
import com.shimao.celnet.payment.service.PPayDaySumService;
import com.shimao.celnet.payment.service.SCodeNameService;
import com.shimao.celnet.payment.util.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/invoice")
public class payParkDaySumInfoPush implements Job {

    @Autowired
    private PPayDaySumService pPayDaySumService;

    @Autowired
    private PInvoiceDaySumService pInvoiceDaySumService;

    @Autowired
    private SCodeNameService sCodeNameService;

    @Autowired
    private HttpSendPostUtil httpSendPostUtil;

    @Autowired
    private ConstantConfig constantConfig;

    @Autowired
    private GetJFSystemToken getJFSystemToken;

    protected Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        /**
         * 数据推送
         * 1，推送的时间范围在当天
         * 2，每天凌晨5推送前一天的数据（例如1月2日00:00:00 推送1月1日00:00:00:--24:59:59）
         */
        //调用发票日汇总存储过程进行汇总
        pPayDaySumService.activateProcess("");//调用支付汇总存储过程
        logger.info("--------------------》》》》调用支付汇总存储过程");
        pInvoiceDaySumService.activateProcess("");//调用发票汇总存储过程

        //获取发票汇总数据
        List<CallOut_OutInvoiceSumRequest> pInvoiceSumList = pInvoiceDaySumService.selectSumInfo("");

        //获取汇总数据
        List<CallOut_OutPaySumRequest> pPayDaySumList = pPayDaySumService.selectSumInfo("","");
        //获取汇总明细数据
        List<CallOut_OutPaySumRequest> pPayDayDetailSumList = pPayDaySumService.selectSumInfo1("1","");
        if (CollectionUtils.isNotEmpty(pPayDaySumList)) {
            logger.info("--------------------》》》》有推送信息");
           for (CallOut_OutPaySumRequest pPayDaySum : pPayDaySumList){
               JSONObject object = new JSONObject();
               JSONArray jsonArray = new  JSONArray();
               object.put("sfId",pPayDaySum.getEstateSfGuid());
               logger.info("--------------------》》》》进入"+pPayDaySum.getEstateSfGuid()+"小区");
               //日汇总支付金额=支付金额-退款金额
               float amount =Float.valueOf(pPayDaySum.getPayAmount()) - Float.valueOf(pPayDaySum.getRefundAmount());
               object.put("amount",amount);
               object.put("orderDate", MyDateUtil.getPreOneDayYmd());//当前时间的前一天
               for (CallOut_OutInvoiceSumRequest callOut_outInvoiceSumRequest : pInvoiceSumList){

                   if(pPayDaySum.getEstateSfGuid().equals(callOut_outInvoiceSumRequest.getEstateSfGuid())){
                       //税额=蓝票税额-蓝票作废税额-红票税额+红票作废税额
                       float totalTax = Float.valueOf(callOut_outInvoiceSumRequest.getBlueInvoiceTotalTax())-Float.valueOf(callOut_outInvoiceSumRequest.getInvlidBlueInvoiceTotalTax())-Float.valueOf(callOut_outInvoiceSumRequest.getRedInvoiceTotalPriceTax())+Float.valueOf(callOut_outInvoiceSumRequest.getInvlidRedInvoiceTotalTax());
                       object.put("totalTax",totalTax);
                       //金额不含税=蓝票金额不含税-蓝票作废金额不含税-红票金额不含税+红票作废金额不含税
                       float totalPrice = Float.valueOf(callOut_outInvoiceSumRequest.getBlueInvoiceTotalPrice())-Float.valueOf(callOut_outInvoiceSumRequest.getInvlidBlueInvoiceTotalPrice())-Float.valueOf(callOut_outInvoiceSumRequest.getRedInvoiceTotalPrice())+Float.valueOf(callOut_outInvoiceSumRequest.getInvlidRedInvoiceTotalPrice());
                       object.put("totalPrice",totalPrice);
                   }
               }
                   for(CallOut_OutPaySumRequest callOut_outPaySumRequest : pPayDayDetailSumList){
                       logger.info("--------------------》》》》进入汇总明细");
                       JSONObject jsonObject = new JSONObject();
                       logger.info("--------------------》》》》进入明细"+callOut_outPaySumRequest.getEstateSfGuid()+"小区");
                   if(pPayDaySum.getEstateSfGuid().equals(callOut_outPaySumRequest.getEstateSfGuid())){
                       logger.info("--------------------》》》》将明细"+callOut_outPaySumRequest.getEstateSfGuid()+"小区,放入------》》》》进入汇总"+pPayDaySum.getEstateSfGuid());
                       if("A01".equals(callOut_outPaySumRequest.getPayType())){
                           jsonObject.put("payType","15");//支付宝
                       }else if("A02".equals(callOut_outPaySumRequest.getPayType())){
                           jsonObject.put("payType","16");//微信
                       }else if("A04".equals(callOut_outPaySumRequest.getPayType())){
                           jsonObject.put("payType","0");//现金
                       }
                       jsonObject.put("amount",callOut_outPaySumRequest.getPayAmount());
                       String type;
                       //park-101-临停支付
                       if("park-101".equals(callOut_outPaySumRequest.getBusinessType())){
                           jsonObject.put("type","1");//临停
                       }else if("park-102".equals(callOut_outPaySumRequest.getBusinessType())){
                           //park-102-月租支付
                           jsonObject.put("type","2");//月租
                       }
                       jsonObject.put("payTime",callOut_outPaySumRequest.getPayDate());
                       jsonArray.add(jsonObject);
                   }
               }
               object.put("detail",jsonArray);

               System.out.println(object);
                /**
                 * 物业计费的接口验证规则
                 * 只接受来自物业的费用
                 */
               String token = getJFSystemToken.getJFSystemNewToken("20180117", "支付系统推送");
                //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                String ret = httpSendPostUtil.sendPost(sCodeNameService.getBWListByType(FinalUtil.CODE_NAME_INVOICE_POST_PARK_URL).get(0).getCode(), object, token,"20180117","支付系统推送");
               System.out.println(ret);
           }

        } else {
            System.out.println("暂无推送消息");
        }
    }
    @RequestMapping(value = "/push/hander",method = RequestMethod.GET)
    public String execute(@RequestParam("strDate") String strDate) {
        /**
         * 数据推送
         * 1，推送的时间范围在当天
         * 2，每天凌晨3推送前一天的数据（例如1月2日00:00:00 推送1月1日00:00:00:--24:59:59）
         */
        //调用发票日汇总存储过程进行汇总
        if(strDate == null || strDate == ""){
            return "[strDate]推送时间为空";
        }
        pPayDaySumService.activateProcess(strDate);//调用支付汇总存储过程

        pInvoiceDaySumService.activateProcess(strDate);//调用发票汇总存储过程

        //获取发票汇总数据
        List<CallOut_OutInvoiceSumRequest> pInvoiceSumList = pInvoiceDaySumService.selectSumInfo(strDate);

        //获取汇总数据
        List<CallOut_OutPaySumRequest> pPayDaySumList = pPayDaySumService.selectSumInfo("",strDate);
        //获取汇总明细数据
        List<CallOut_OutPaySumRequest> pPayDayDetailSumList = pPayDaySumService.selectSumInfo("1",strDate);
        String ret = strDate+"无推送数据";
        if (CollectionUtils.isNotEmpty(pPayDaySumList)) {
            System.out.println("有推送信息");
            JSONObject object = new JSONObject();
            JSONArray jsonArray = new  JSONArray();
            for (CallOut_OutPaySumRequest pPayDaySum : pPayDaySumList){
                object.put("sfId",pPayDaySum.getEstateSfGuid());
                //日汇总支付金额=支付金额-退款金额
                float amount =Float.valueOf(pPayDaySum.getPayAmount()) - Float.valueOf(pPayDaySum.getRefundAmount());
                object.put("amount",amount);
                object.put("orderDate",pPayDaySum.getPayDate());
                for (CallOut_OutInvoiceSumRequest callOut_outInvoiceSumRequest : pInvoiceSumList){
                    if(pPayDaySum.getEstateSfGuid().equals(callOut_outInvoiceSumRequest.getEstateSfGuid())){
                        //税额=蓝票税额-蓝票作废税额-红票税额+红票作废税额
                        float totalTax = Float.valueOf(callOut_outInvoiceSumRequest.getBlueInvoiceTotalTax())-Float.valueOf(callOut_outInvoiceSumRequest.getInvlidBlueInvoiceTotalTax())-Float.valueOf(callOut_outInvoiceSumRequest.getRedInvoiceTotalPriceTax())+Float.valueOf(callOut_outInvoiceSumRequest.getInvlidRedInvoiceTotalTax());
                        object.put("totalTax",totalTax);
                        //金额不含税=蓝票金额不含税-蓝票作废金额不含税-红票金额不含税+红票作废金额不含税
                        float totalPrice = Float.valueOf(callOut_outInvoiceSumRequest.getBlueInvoiceTotalPrice())-Float.valueOf(callOut_outInvoiceSumRequest.getInvlidBlueInvoiceTotalPrice())-Float.valueOf(callOut_outInvoiceSumRequest.getRedInvoiceTotalPrice())+Float.valueOf(callOut_outInvoiceSumRequest.getInvlidRedInvoiceTotalPrice());
                        object.put("totalPrice",totalPrice);
                    }
                }
                for(CallOut_OutPaySumRequest callOut_outPaySumRequest : pPayDayDetailSumList){
                    JSONObject jsonObject = new JSONObject();
                    if(pPayDaySum.getEstateSfGuid().equals(callOut_outPaySumRequest.getEstateSfGuid())){
                        if("A01".equals(callOut_outPaySumRequest.getPayType())){
                            jsonObject.put("payType","15");//支付宝
                        }else if("A02".equals(callOut_outPaySumRequest.getPayType())){
                            jsonObject.put("payType","16");//微信
                        }else if("A04".equals(callOut_outPaySumRequest.getPayType())){
                            jsonObject.put("payType","0");//现金
                        }
                        jsonObject.put("amount",callOut_outPaySumRequest.getPayAmount());
                        String type;
                        //park-101-临停支付
                        if("park-101".equals(callOut_outPaySumRequest.getBusinessType())){
                            jsonObject.put("type","1");//临停
                        }else if("park-102".equals(callOut_outPaySumRequest.getBusinessType())){
                            //park-102-月租支付
                            jsonObject.put("type","2");//月租
                        }
                        jsonArray.add(jsonObject);
                    }
                }
                object.put("detail",jsonArray);

                System.out.println(object);
                /**
                 * 物业计费的接口验证规则
                 * 只接受来自物业的费用
                 */
                String token = getJFSystemToken.getJFSystemNewToken("20180117", "支付系统推送");
                System.out.println("********" + token);
                //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                ret = httpSendPostUtil.sendPost(sCodeNameService.getBWListByType(FinalUtil.CODE_NAME_INVOICE_POST_PARK_URL).get(0).getCode(), object, token,"20180117","支付系统推送");
                System.out.println(ret);
            }
            return "推送成功"+ret;

        } else {
            System.out.println("暂无推送消息");
            return ret;
        }
    }
}
